package objects;

import java.util.Iterator;
import java.util.Vector;

public class Trd {

	private Vector <Message> messages;
	private String _nameOfThread;
	private Message findMessage (String str)
	{
		
		for (Iterator iterator = this.messages.iterator(); iterator.hasNext();) {
			Message msg = (Message) iterator.next();
			if(msg.getTitleOfMessage().trim().equals(str.trim()))
				return msg;
		}
		return null;
	}
	public Trd(String nameOfThread)
	{
		this._nameOfThread=nameOfThread;
		messages =new Vector<Message>();
	}
	public void addMessage(User u,String msg)
	{
		messages.add(new Message(u,msg));
	}
	/**
	 * remove message from thread
	 * @param msg the string of the message 
	 * @return 1 if remove , -1 if message doesn't found
	 */
	public int removeMessage(String msg)
	{
		Message message=findMessage(msg);
		if (message!=null)
		{
			this.messages.remove(message);
			return 1;
		}
		else return -1;
		
	}
	public String get_nameOfThread() {
		return _nameOfThread;
	}
	public void set_nameOfThread(String ofThread) {
		_nameOfThread = ofThread;
	}
	/**
	 * @return the messages
	 */
	public Vector<Message> getMessages() {
		return messages;
	}
	@Override
	public String toString()
	{
		return _nameOfThread;
	}
	
	
}
